package yxw.com.smartbutler;

import android.content.Intent;
import android.support.annotation.Nullable;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import yxw.com.smartbutler.fragment.BulterFragment;
import yxw.com.smartbutler.fragment.GirlFragment;
import yxw.com.smartbutler.fragment.UserFragment;
import yxw.com.smartbutler.fragment.WechatFragment;
import yxw.com.smartbutler.ui.SettingActivity;
import yxw.com.smartbutler.utils.L;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //Tablayout
    private TabLayout mTabLayout;

    //ViewPage
    private ViewPager mViewPage;

    //Title
    private List<String> mTitle;

    //Fragment
    private List<Fragment> mFragment;

    //悬浮窗
    private FloatingActionButton mfabSetting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //去掉阴影
        getSupportActionBar().setElevation(0);

        initData();
        initView();

      //  CrashReport.testJavaCrash();
    }

    /**
     * 初始化数据
     */
    private void initData() {

        mTitle = new ArrayList<String>();
        mTitle.add("服务管家");
        mTitle.add("微信精选");
        mTitle.add("美女社区");
        mTitle.add("个人中心");

        mFragment = new ArrayList<Fragment>();
        mFragment.add(new BulterFragment());
        mFragment.add(new GirlFragment());
        mFragment.add(new UserFragment());
        mFragment.add(new WechatFragment());


    }

    /**
     * 初始化view
     */
    private void initView() {
        mfabSetting = (FloatingActionButton) findViewById(R.id.fab_setting);
        mfabSetting.setOnClickListener(this);
        //默认隐藏
        mfabSetting.setVisibility(View.GONE);

        mTabLayout = findViewById(R.id.mTablayout);
        mViewPage = findViewById(R.id.mViewPage);

//预加载
        mViewPage.setOffscreenPageLimit(mFragment.size());
//滑动监听
        mViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i("TAG", "position:" + position);
                if (position == 0) {
                    mfabSetting.setVisibility(View.GONE);
                } else {
                    mfabSetting.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
//设置适配器
        mViewPage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            /**
             * 选中的item
             * @param position
             * @return
             */
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            /**
             * 返回item的个数
             * @return
             */
            @Override
            public int getCount() {
                return mFragment.size();
            }

            /**
             * 设置标题
             * @param position
             * @return
             */
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {

                return mTitle.get(position);
            }
        });

        //绑定
        mTabLayout.setupWithViewPager(mViewPage);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fab_setting:
                startActivity(new Intent(this, SettingActivity.class));
                break;
        }
    }
}
